package co.yixiang.modules.system.rest.member;

import co.yixiang.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import cn.hutool.core.convert.Convert;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.system.domain.ConsumerOrder;
import co.yixiang.modules.system.service.ConsumerOrderService;
import co.yixiang.modules.system.dto.queryCriteria.ConsumerOrderQueryCriteria;
import co.yixiang.modules.system.dto.ConsumerOrderDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
* @author zf
* @date 2020-08-15
*/
@AllArgsConstructor
@Api(tags = "客户订单管理")
@RestController
@RequestMapping("/member/consumerOrder")
public class ConsumerOrderController {

    private final ConsumerOrderService consumerOrderService;

    @Log("获取用户订单")
    @ApiOperation("导出数据")
    @GetMapping(value = "/get-consumer-orders-by-user")
    public ResponseEntity<Map<String, Object>> getConsumerOrdersByUser(ConsumerOrderQueryCriteria criteria, Pageable pageable) {
		int uid = SecurityUtils.getUserId().intValue();
		criteria.setConsumerId(uid);
		return new ResponseEntity<>(consumerOrderService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','consumerOrder:list')")
    public void download(HttpServletResponse response, ConsumerOrderQueryCriteria criteria) throws IOException {
        consumerOrderService.download(Convert.toList(ConsumerOrderDto.class, consumerOrderService.queryAll(criteria)), response);
    }

    @GetMapping
    @Log("查询客户订单")
    @ApiOperation("查询客户订单")
    @PreAuthorize("@el.check('admin','consumerOrder:list')")
    public ResponseEntity<Object> getConsumerOrders(ConsumerOrderQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(consumerOrderService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增客户订单")
    @ApiOperation("新增客户订单")
    @PreAuthorize("@el.check('admin','consumerOrder:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ConsumerOrder resources){
        return new ResponseEntity<>(consumerOrderService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改客户订单")
    @ApiOperation("修改客户订单")
    @PreAuthorize("@el.check('admin','consumerOrder:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ConsumerOrder resources){
        consumerOrderService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除客户订单")
    @ApiOperation("删除客户订单")
    @PreAuthorize("@el.check('admin','consumerOrder:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody List<Integer> ids) {
        consumerOrderService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
